package cn.acewill.yxd.yunxundian.utils;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.media.ExifInterface;

import java.io.IOException;

/**
 * 专门用于处理图片相关操作的工具类
 */
public class BitmapUtils {

	/**
	 * 将指定SD路径中的某张图片进行压缩
	 *
	 * @param imgPath
	 *            要进行缩放的图片路径
	 * @param reqWidth
	 *            缩放后的宽度
	 * @param reqHeight
	 *            缩放后的高度
	 * @return 压缩处理后的图片
	 */
	public static Bitmap decodeSampledBitmap(String imgPath, int reqWidth,
			int reqHeight) {
		final BitmapFactory.Options options = new BitmapFactory.Options();
		options.inJustDecodeBounds = true;
		BitmapFactory.decodeFile(imgPath, options);
		options.inSampleSize = calculateInSampleSize(options, reqWidth,
				reqHeight);
		options.inJustDecodeBounds = false;
		return BitmapFactory.decodeFile(imgPath, options);
	}

	/**
	 * 将指定drawable中的图片进行压缩显示
	 *
	 * @param res
	 *            Resource参数
	 * @param resId
	 *            要处理的drawable中的图片ID
	 * @param reqWidth
	 *            压缩后的图片宽度
	 * @param reqHeight
	 *            压缩后的图片高度
	 * @return 返回压缩后的图片
	 *
	 */
	public static Bitmap decodeSampledBitmap(Resources res, int resId,
			int reqWidth, int reqHeight) {

		/*
		 * BitmapFactory.Options : 关于对图片缩放或图片质量改变时的�?��属�?设置
		 * 
		 * options.inJustDecodeBounds �?获取图片信息时，是否要将原图返回（是否要将图片先保存在内存中再来获取图片信息�?
		 * 
		 * options.inSampleSize : 将图片进行压缩，压缩比例�? 压缩图片都是位图 压缩比例，分别压缩的�?图片原始的宽和高
		 * 如： inSampleSize = 2 ; 则压缩后的图片宽= 原始�?2 ; �? 原始�?/2
		 */
		BitmapFactory.Options options = new BitmapFactory.Options();
		options.inJustDecodeBounds = true;// 当把这个�?设置为true时，仅仅获取的原始图片的宽�?高信息，而不将这个图片保存到内存
		// 将resId的图片转换为bitmap格式图片（当将options.inJustDecodeBounds设置为true时，此时调用下面这个方法，会返回�?��null，即图片不会返回，只是返回的图片的相关信息（如：宽�?高�?大小等）
		BitmapFactory.decodeResource(res, resId, options);

		options.inSampleSize = calculateInSampleSize(options, reqWidth,
				reqHeight);
		options.inJustDecodeBounds = false;

		return BitmapFactory.decodeResource(res, resId, options);
	}


	private static int calculateInSampleSize(BitmapFactory.Options options,
			int reqWidth, int reqHeight) {
		final int height = options.outHeight;
		final int width = options.outWidth;
		int inSampleSize = 1;

		if (height > reqHeight || width > reqWidth) {
			final int heightRatio = Math.round((float) height
					/ (float) reqHeight);
			final int widthRatio = Math.round((float) width / (float) reqWidth);

			inSampleSize = heightRatio > widthRatio ? heightRatio : widthRatio;
		}
		return inSampleSize;
	}

	/**
	 * 按指定的角度来旋转原图片
	 */
	public static Bitmap rotateBitmap(Bitmap bitmap, int degress) {
		if (bitmap != null) {
			Matrix m = new Matrix();
			m.postRotate(degress);
			bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
					bitmap.getHeight(), m, true);
			return bitmap;
		}
		return bitmap;
	}

	/**
	 * 获取照片的拍摄角度?
	 */
	public static int readPictureDegree(String path) {
		int degree = 0;
		try {
			ExifInterface exifInterface = new ExifInterface(path);
			int orientation = exifInterface.getAttributeInt(
					ExifInterface.TAG_ORIENTATION,
					ExifInterface.ORIENTATION_UNDEFINED);
			switch (orientation) {
			case ExifInterface.ORIENTATION_ROTATE_90:
				degree = 90;
				break;
			case ExifInterface.ORIENTATION_ROTATE_180:
				degree = 180;
				break;
			case ExifInterface.ORIENTATION_ROTATE_270:
				degree = 270;
				break;
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return degree;
	}

	/**
	 * 获取圆角的bitmap
	 * 
	 * @param bitmap
	 *            要设置的原始图片
	 * @param pixels
	 *            要设置的图片四个角的弧度半径值，这里设置的是象素
	 * @return 转换后的图片
	 */
	public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int pixels) {

		Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
				bitmap.getHeight(), Config.ARGB_8888);
		Canvas canvas = new Canvas(output);

		final int color = 0xff424242;
		final Paint paint = new Paint();
		final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
		final RectF rectF = new RectF(rect);
		final float roundPx = pixels;

		paint.setAntiAlias(true);
		canvas.drawARGB(0, 0, 0, 0);
		paint.setColor(color);
		canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

		paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
		canvas.drawBitmap(bitmap, rect, rect, paint);

		return output;
	}

	public static int getPixelsFromDipValue(int dip) {

		return 0;
	}
}
